<template>
<transition name="el-zoom-in-center">
<div class="JNPF-preview-main">
    <div class="JNPF-common-page-header">
        <el-page-header @back="goBack"
                        :content="!dataForm.id ? '新建':'编辑'"/>
        <div class="options">
            <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 保 存</el-button>
            <el-button @click="goBack"> 取 消</el-button>
        </div>
    </div>
<el-row :gutter="15" class=" main" :style="{margin: '0 auto',width: '100%'}">
<el-form ref="elForm" :model="dataForm" :rules="rules" size="small" label-width="100px" label-position="right" >
    <template v-if="!loading">
	    <el-col :span="24" >
		<el-form-item  label-width="0">
		<groupTitle  content-position="center"  :closable= "false"  content ="单据转换" >
		</groupTitle>
	</el-form-item>
	</el-col>
            <el-col :span="24"  >
                <el-form-item  label="来源表名"   prop="sourcetable" >
                    <el-select   v-model="dataForm.sourcetable" placeholder="请选择"  clearable  :style='{"width":"100%"}' filterable >
                            <el-option v-for="(item, index) in sourcetableOptions" :key="index" :label="item.tableName" :value="item.table" :disabled="item.disabled" ></el-option>

                </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="来源表名描述"   prop="sourcemark" >
                    <el-input   v-model="dataForm.sourcemark" placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="来源单据类型"   prop="sourcetypecode" >
                    <el-input   v-model="dataForm.sourcetypecode" placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="目标表名"   prop="targettable" >
                    <el-select   v-model="dataForm.targettable" placeholder="请选择"  clearable  :style='{"width":"100%"}' filterable >
                            <el-option v-for="(item, index) in targettableOptions" :key="index" :label="item.tableName" :value="item.table" :disabled="item.disabled" ></el-option>

                </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="目标表名描述"   prop="targetmark" >
                    <el-input   v-model="dataForm.targetmark" placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="目标单据类型"   prop="targettypecode" >
                    <el-input   v-model="dataForm.targettypecode" placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="描述"   prop="description" >
                    <el-input   v-model="dataForm.description" placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
            <el-divider   content-position="center" >
尾部信息         </el-divider>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="所属组织"   prop="org" >
                    <com-select   v-model="dataForm.org" placeholder="请选择" >

                </com-select>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="创建用户"   prop="creatoruserid" >
                    <jnpf-open-data   v-model="dataForm.creatoruserid" readonly  type="currUser" >

                </jnpf-open-data>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="创建时间"   prop="creatortime" >
                    <jnpf-open-data   v-model="dataForm.creatortime" readonly  type="currTime" >

                </jnpf-open-data>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="修改用户"   prop="lastmodifyuserid" >
                    <el-input   v-model="dataForm.lastmodifyuserid" placeholder="系统自动生成"  readonly >

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="修改时间"   prop="lastmodifytime" >
                    <el-input   v-model="dataForm.lastmodifytime" placeholder="系统自动生成"  readonly >

                </el-input>
                </el-form-item>
            </el-col>
    </template>
</el-form>
    <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm"
              ref="selectDialog" @select="addForSelect" @close="selectDialogVisible=false"/>
    </el-row>
    </div>
</transition>
</template>
<script>
    import request from '@/utils/request'
    import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'

    export default {
        components: {},
        props: [],
        data() {
            return {
                visible: false,
                loading: false,
                btnLoading: false,
                selectDialogVisible: false,
                currTableConf:{},
                addTableConf:{
            },
                tableRows:{
                },
            currVmodel:"",
            dataForm: {
                                                            sourcetable : "",
                                                                sourcemark : '',
                                                                sourcetypecode : '',
                                                            targettable : "",
                                                                targetmark : '',
                                                                targettypecode : '',
                                                                description : '',
                                                            org : [],
                                                            creatoruserid : "",
                                                            creatortime : "",
                                                            lastmodifyuserid : "",
                                                            lastmodifytime : "",
                                                                id : '',
                    version: 0,
            },
            rules:
            {
                            sourcetable: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],
                            targettable: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],



            },
                        sourcetableOptions:[],
                        targettableOptions:[],

        }
        },
        computed: {},
        watch: {},
        created() {
                    this.getsourcetableOptions();
                    this.gettargettableOptions();
        },
        mounted() {},
        methods: {
                    getsourcetableOptions() {
                        getDataInterfaceRes('375647591695648069').then(res => {
                            let data = res.data
                            this.sourcetableOptions = data
                        })
                    },
                    gettargettableOptions() {
                        getDataInterfaceRes('375647591695648069').then(res => {
                            let data = res.data
                            this.targettableOptions = data
                        })
                    },
                goBack() {
                    this.$emit('refresh')
                },
            clearData(){
                               this.dataForm.sourcetable = "";
                               this.dataForm.sourcemark = '';
                               this.dataForm.sourcetypecode = '';
                               this.dataForm.targettable = "";
                               this.dataForm.targetmark = '';
                               this.dataForm.targettypecode = '';
                               this.dataForm.description = '';
                                 this.dataForm.org = [];
                               this.dataForm.creatoruserid = "";
                               this.dataForm.creatortime = "";
                               this.dataForm.lastmodifyuserid = "";
                               this.dataForm.lastmodifytime = "";
                               this.dataForm.id = '';
                    this.dataForm.version= 0;
            },
            init(id) {
                this.dataForm.id = id || 0;
                this.visible = true;
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if(this.dataForm.id){
                        this.loading = true
                        request({
                            url: '/api/example/SmBillchange/'+this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataInfo(res.data)
                            this.loading = false
                        });
                    }else{
                       this.clearData()
                    }
                });
                this.$store.commit('generator/UPDATE_RELATION_DATA', {})
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                            this.request()
                    }
                })
            },
            request() {
                this.btnLoading = true
                let _data =this.dataList()
                if (!this.dataForm.id) {
                    request({
                        url: '/api/example/SmBillchange',
                        method: 'post',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }else{
                    request({
                        url: '/api/example/SmBillchange/'+this.dataForm.id,
                        method: 'PUT',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }
            },
            openSelectDialog(key) {
                this.currTableConf=this.addTableConf[key]
                this.currVmodel=key
                this.selectDialogVisible = true
                this.$nextTick(() => {
                    this.$refs.selectDialog.init()
                })
            },
            addForSelect(data) {
                for (let i = 0; i < data.length; i++) {
                    let item={...this.tableRows[this.currVmodel],...data[i]}
                    this.dataForm[this.currVmodel].push(item)
                }
            },
            dataList(){
                var _data = JSON.parse(JSON.stringify(this.dataForm));
        _data.org = Array.isArray(_data.org)? JSON.stringify(_data.org):'[]'
                return _data;
            },
            dataInfo(dataAll){
                let _dataAll =dataAll
        _dataAll.org = _dataAll.org ?JSON.parse(_dataAll.org):[]
                this.dataForm = _dataAll
            },
        },
    }

</script>
